package t327.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import t327.mapper.HomePageRenderingMapper;
import t327.mapper.SewingManageMapper;
import t327.pojo.*;
import t327.pojo.views.PlanOrderProductionStyleView;
import t327.pojo.views.SubmissionOrderProductStyleDepartmentSizeView;
import t327.service.iter.ISewingManagerService;

import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class SewingManagerServiceImpl implements ISewingManagerService {
    @Autowired
    private SewingManageMapper sewingManagerMapper;
    @Autowired
    private HomePageRenderingMapper homePageRenderingMapper;
    @Override
    public List<PlanOrderProductionStyleView> getAllPlanOrderSizeView() {
        return sewingManagerMapper.getAllPlanOrderSizeView();
    }

    @Override
    public HashMap<String, String> addSubmission(Submission submission) {
        HashMap<String,String> map = new HashMap<>();
        //判断计划生产数量和生产数量是否一致
        Long productionPlanId = submission.getProductionPlanId();
        ProductionPlan productById = sewingManagerMapper.getProductById(productionPlanId);
        if (productById == null){
            map.put("code","206");
            map.put("msg","没有该计划列表");
            return map;
        }
        Long schedeleCount = productById.getSchedeleCount();
        Long producedCount1 = submission.getProducedCount();
        //判断该条订单是否需要继续生产
        if (producedCount1 > schedeleCount){
            map.put("code","206");
            map.put("msg","超额生产,该条生产订单不需要这么多");
            return map;
        }
        List<Submission> submissionCountByPlanId = sewingManagerMapper.getSubmissionCountByPlanId(productionPlanId);
        long totalNum = 0;
        long submissionCount = 0;
        if (submissionCountByPlanId != null && !submissionCountByPlanId.isEmpty()) {
            // 进行计算
            for (int i = 0; i < submissionCountByPlanId.size() ; i++) {
                Long producedCount = submissionCountByPlanId.get(i).getProducedCount();
                submissionCount += producedCount;
            }
            //判断生产数量和计划生产的数量
            if (submissionCount >= schedeleCount){
                map.put("code","206");
                map.put("msg","该条生产订单已经达标不需要再生产");
                return map;
            }
            totalNum = submissionCount + producedCount1;
            //判断该条订单是否需要继续生产
            if (schedeleCount < totalNum){
                map.put("code","206");
                map.put("msg","第  "+productionPlanId+"  条计划生产线比预计多生产"+(totalNum-schedeleCount)+"  件,实际还剩  "+(schedeleCount-submissionCount)+
                        "  可以生产");
                return map;
            }
            submission.setDepartmentId(2l);
            int i = sewingManagerMapper.addSubmission(submission);
            if(i>0){
                map.put("code","200");
                map.put("msg","添加成功");
                return map;
            }else {
                map.put("code","205");
                map.put("msg","添加失败");
                return map;
            }
        } else {
            // 处理列表为空的情况
            map.put("code","206");
            map.put("msg","没有该计划列表");
            return map;
        }
    }

    @Override
    public List<SubmissionOrderProductStyleDepartmentSizeView> queryAllSubmission() {
        return sewingManagerMapper.queryAllSubmission();
    }
    @Override
    public List<Submission> QueryTheNumberOfCropProduction() {
        return homePageRenderingMapper.QueryTheNumberOfCropProduction();
    }
}
